package org.jeecg.modules.app.utils.encrypt;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author tdkj
 */
public class AesUtils {
    public static final String bm = "utf-8";

    /**
     * tdkjyfecb MD5
     */
    private static String AesEBCKey = "6283f69e412c03b9";
    /**
     * tdkjyfkey MD5
     */
    private static String AesKey = "dd6a083da4d49e90";
    /**
     * tdkjyfiv MD5
     */
    private static String AesIv = "9bcd461ea230eea7";
    /**
     * AES 加密
     *
     * @param content
     *            明文
     * @return
     */

    public static String encrypt(String content) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(AesIv.getBytes());
            SecretKeySpec key = new SecretKeySpec(AesKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(content.getBytes(bm));

            return new String(Base64.encode(encryptedData));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES 解密
     *
     * @param content
     *            密文
     * @return
     */

    public static String decrypt(String content) {
        try {
            byte[] byteMi = Base64.decode(content.getBytes());
            IvParameterSpec zeroIv = new IvParameterSpec(AesIv.getBytes());
            SecretKeySpec key = new SecretKeySpec(AesKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, "utf-8");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 加密
    public static String encrypt_ecb(String sSrc) {
        try {
            byte[] raw = AesEBCKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

            return new String(Base64.encode(encrypted));
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // 解密
    public static String decrypt_ecb(String sSrc) {
        try {
            byte[] raw = AesEBCKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decode(sSrc.getBytes());//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static void main(String... args) {
        System.out.println(AesUtils.encrypt("tdkjyf.android.."));
        System.out.println(AesUtils.encrypt_ecb("tdkj"));
        System.out.println(AesUtils.encrypt_ecb("1"));

        System.out.println(AesUtils.decrypt("chuvzOo9cvA3lXZCUwICUg=="));

    }
}
